﻿using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
namespace INTEX.DataBase.Tools
{
    public partial class INTEXDBC
    {
        public DataSet QueryKDANSP(DataRow dwData, int option = 1)
        {
            DataSet setReturn = new DataSet();
            DataSet setResult = new DataSet();
            OracleCommand or1;

            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KDANSP WHERE  ROWNUM<2 ";
                    break;
                case 1:
                    or1.CommandText = "SELECT * FROM KDANSP   WHERE "
                            + " KDSKUNDNR = :KDSKUNDNR";
                    or1.Parameters.Add("KDSKUNDNR", OracleType.NVarChar).Value = dwData["KDSKUNDNR"].ToString();
                    break;
                case 2:
                    or1.CommandText = "   SELECT DISTINCT VVLIEFSCH,C.* FROM VKPOS P JOIN VKKOPF ON VKKAUFKRS=VKPAUFKRS AND VKKAUFLFD=VKPAUFLFD JOIN VVTLIEF ON VVTAUFTYP='K' AND VKPSA='  ' AND VVTAUFKRS=VKPAUFKRS AND VVTAUFLFD=VKPAUFLFD AND VVTAUFPOS=VKPAUFPOS  JOIN VVLIEF ON VVLIEFSCH=VVTLIEFSCH   JOIN KDANSP C ON VKKKUNDNR=KDSKUNDNR AND VKKANSPRECH=KDSANSPNA    WHERE  VVNEUDAT>='" + dwData["DSTART"].ToString() + "' AND VVNEUDAT<='" + dwData["DEND"].ToString() + "' AND VVSTATUS IN (" + dwData["C20"].ToString() + ")";                    
                    break;
                case 3:
                    or1.CommandText = "SELECT * FROM KDANSP  JOIN VKKOPF ON KDSMANDANT=VKKMANDANT AND KDSKUNDNR=VKKKUNDNR WHERE VKKMANDANT=:VKKMANDANT AND VKKAUFKRS=:VKKAUFKRS AND VKKAUFLFD=:VKKAUFLFD";
                    or1.Parameters.Add("VKKMANDANT", OracleType.NVarChar).Value = dwData["VKKMANDANT"].ToString();
                    or1.Parameters.Add("VKKAUFKRS", OracleType.NVarChar).Value = dwData["VKKAUFKRS"].ToString();
                    or1.Parameters.Add("VKKAUFLFD", OracleType.NVarChar).Value = dwData["VKKAUFLFD"].ToString();
                    break;
            } 
            setResult = Connect2Proxy(or1);
            if (setResult.Tables.Count > 0)
            {
                setResult.Tables[0].TableName = "KDANSP";
            }
            setReturn.Merge(setResult);

            return setReturn;
        }
    }
}
